﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.Statistics;
using MINERP.MODEL.FoundationModel;
using MINERP.ComControls.Extension;

namespace MINERP.Statistics.Forms
{
    public partial class FrmDivert : Form
    {
        public FrmDivert(int inBillId, int inDetailId)
        {
            InitializeComponent();
            intFromBillid = inBillId;
            intFromDetailID = inDetailId;

            gridViewProduct.ShowRowIndicator(50);
            gridViewToBill.ShowRowIndicator(50);
            gridViewToDetail.ShowRowIndicator(50);
        }
        int intFromBillid = -1;
        int intFromDetailID = -1;

        private string userName = FrmMain.loginName;
        BillEditBLL _bll = new BillEditBLL();
        private void FrmDivert_Load(object sender, EventArgs e)
        {
            DisplayFromBill();
        }
        BillView billView = new BillView();
        List<BillDetailView> listDetail = new List<BillDetailView>();

        void DisplayFromBill()
        {
            billView = _bll.GetBillById(intFromBillid);

            textBoxFromShopName.Text = billView.ShopName;
            textBoxBillCode.Text = billView.BillOrderCode.ToString();
            textBoxBillID.Text = billView.BillId.ToString();
            textBoxCount.Text = billView.BillMoneyCount.ToString("0.00");
            listDetail = _bll.GetBillDetailList(intFromBillid);
            listDetail.ForEach(f=>f.IsSelect=false);
            listDetail.FirstOrDefault(d => d.BillDetailId == intFromDetailID).IsSelect = true;
            gridControlProduct.DataSource = listDetail;
            //gridControlProduct.DataBindings();


            comboBoxshop.DataSource = _bll.GetShopList();
            comboBoxshop.DisplayMember = "ShopName";
            comboBoxshop.ValueMember = "ShopId";
        }



        private void gridViewProduct_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (intFromDetailID != 0)
                {
                    BillDetailView detailView = (BillDetailView)gridViewProduct.GetRow(e.RowHandle);
                    if (detailView.BillDetailId == intFromDetailID)
                    {
                        e.Appearance.BackColor = Color.Red;
                    }
                }
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (intFromDetailID == 0)//整张账单转移
            {
                if (DialogResult.OK == MessageBox.Show("注意：按“账单生成月份转移”点“确定”，按“当前月份转移”点“取消”", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
                {
                    _bll.DivertBill(intFromBillid, ((Shop_Info)comboBoxshop.SelectedItem).ShopId, billView.CreateTime,userName);

                }
                else
                {
                    _bll.DivertBill(intFromBillid, ((Shop_Info)comboBoxshop.SelectedItem).ShopId, DateTime.Now,userName);
                }
                MessageBox.Show("账单转移成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                gridControlTOBill.RefreshDataSource();
                this.Close();

                //_bll.DivertBill(int inFromBillID,int inToBillID,DateTime inDate);

            }
            else//某产品转移
            {
                if (intCurrBillID != 0)//目标月份订单
                {
                    if (listDetail.Count() == 1)//当只有一个产品的账单转移时。按照整张账单转移
                    {
                       

                        //_bll.DivertProduct2(intFromBillid, ((Shop_Info)comboBoxshop.SelectedItem).ShopId, billView.CreateTime, intFromDetailID, intCurrBillID);
                        _bll.DivertBill(intFromBillid, ((Shop_Info)comboBoxshop.SelectedItem).ShopId, DateTime.Now, userName);
                        MessageBox.Show("只有一个产品的账单转移成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    else//正常转移产品
                    {
                        _bll.DivertProduct(intFromBillid, intFromDetailID, intCurrBillID, userName);
                        MessageBox.Show("产品转移成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                }
                else//当前月份
                {
                    if (isCreate)
                    {
                        if (checkBoxCurrDate.Checked == true)
                        {
                            _bll.CreateBill(intFromBillid, intFromDetailID, ((Shop_Info)comboBoxshop.SelectedItem).ShopId, DateTime.Now, userName);
                            MessageBox.Show("产品转移成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                        else
                        {
                            _bll.CreateBill(intFromBillid, intFromDetailID, ((Shop_Info)comboBoxshop.SelectedItem).ShopId, billView.CreateTime, userName);
                            MessageBox.Show("产品转移成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                        }
                    }
                    else
                    {
                        if(MessageBox.Show("是否创建新账单", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)==DialogResult.OK)
                        {
                            if (checkBoxCurrDate.Checked == true)
                            {
                                _bll.CreateBill(intFromBillid, intFromDetailID, ((Shop_Info)comboBoxshop.SelectedItem).ShopId, DateTime.Now, userName);
                                MessageBox.Show("产品转移成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                this.Close();
                            }
                            else
                            {
                                _bll.CreateBill(intFromBillid, intFromDetailID, ((Shop_Info)comboBoxshop.SelectedItem).ShopId, billView.CreateTime, userName);
                                MessageBox.Show("产品转移成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                this.Close();
                            }
                        }
                        else
                        {
                            this.Close();
                        }
                    }


                }
            }
            intCurrBillID = 0;
            intFromDetailID = -1;
            isCreate = false;
        }



        int intCurrBillID = 0;
        BillView view = new BillView();
        private void gridViewToBill_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {

            if (e.RowHandle >= 0)
            {

                BillView view = (BillView)gridViewToBill.GetRow(e.RowHandle);
                gridControlToDetai.DataSource = _bll.GetBillDetailList(view.BillId);
                intCurrBillID = view.BillId;

            }
        }
        Boolean isCreate = false;
        //private void buttonSelect_Click(object sender, EventArgs e)
        //{
        //    //int intShopId = ((Shop_Info)comboBoxshop.SelectedItem).ShopId;
        //    //DateTime date = (Convert.ToDateTime(dateTimePickerYearMonth.Text)).Date;
        //    //List<BillView> list=_bll.GetBillByShopAndMonth(intShopId, date);
        //    //gridControlTOBill.DataSource = list;

        //    //if(list.Count()==0)
        //    //{

        //    //    if (DialogResult.OK == MessageBox.Show("该商户本月没有生产账单？确定要转移到该商户？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
        //    //    {
        //    //        isCreate = true;
        //    //    }
        //    //}

        //}

        private void comboBoxshop_DropDownClosed(object sender, EventArgs e)
        {
            int intShopId = ((Shop_Info)comboBoxshop.SelectedItem).ShopId;
            DateTime date = new DateTime();
            if (checkBoxCurrDate.Checked == true)
            {
                date = DateTime.Now;
            }
            else
            {

                date = billView.CreateTime;
            }
            List<BillView> list = _bll.GetBillByShopAndMonth(intShopId, date);
            gridControlTOBill.DataSource = list;
            gridControlToDetai.DataSource = null;
            gridControlToDetai.RefreshDataSource();

            if (list.Count() == 0)
            {
                isCreate = true;

            }


        }


    }
}
